package com.ruoyi.project.modules.interfaceinfo.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.sql.SqlUtil;
import com.ruoyi.project.modules.fileInfo.domain.FileInf;
import com.ruoyi.project.modules.fileInfo.mapper.FileInfoMapper;
import com.ruoyi.project.modules.interfaceGenerator.service.IInterfaceGeneratorService;
import com.ruoyi.project.modules.interfaceModel.mapper.InterfaceModelMapper;
import com.ruoyi.project.modules.interfaceObject.domain.InterfaceObject;
import com.ruoyi.project.modules.interfaceObject.mapper.InterfaceObjectMapper;
import com.ruoyi.project.modules.interfaceinfo.domain.VirtualUsage;
import com.ruoyi.project.modules.interfaceinfo.mapper.VirtualUsageMapper;
import com.ruoyi.project.modules.interfaceinfo.request.*;
import com.ruoyi.project.modules.interfaceinfo.vo.InterfaceParam;
import com.ruoyi.project.modules.parameter.domain.InterfaceParamsFieldRelation;
import com.ruoyi.project.modules.parameter.domain.ParamTree;
import com.ruoyi.project.modules.parameter.domain.ParamsFieldRelation;
import com.ruoyi.project.modules.parameter.mapper.InterfaceParamsFieldRelationMapper;
import com.ruoyi.project.modules.parameter.mapper.ParamsFieldRelationMapper;
import com.ruoyi.project.modules.parameter.mapper.ParamsMapper;
import com.ruoyi.project.modules.productVersionObejct.domain.ProductVersionObejct;
import com.ruoyi.project.modules.productVersionObejct.mapper.ProductVersionObejctMapper;
import com.ruoyi.project.modules.table.domain.FieldInfo;
import com.ruoyi.project.modules.table.domain.TableInfo;
import com.ruoyi.project.modules.table.service.ITableInfoService;
import com.ruoyi.project.modules.usage.domain.Usage;
import com.ruoyi.project.modules.userDataAuth.domain.UserDataAuth;
import com.ruoyi.project.modules.userDataAuth.mapper.UserDataAuthMapper;
import com.ruoyi.system.service.ISysDictTypeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import com.ruoyi.project.modules.interfaceinfo.mapper.InterfaceInfoMapper;
import com.ruoyi.project.modules.interfaceinfo.domain.InterfaceInfo;
import com.ruoyi.project.modules.interfaceinfo.service.IInterfaceInfoService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;

import javax.annotation.Resource;

/**
 * 实体用况Service业务层处理
 *
 * @author smallrain
 * @date 2021-10-29
 */
@Service
public class InterfaceInfoServiceImpl implements IInterfaceInfoService {
    @Resource
    private InterfaceInfoMapper interfaceInfoMapper;
    @Resource
    private FileInfoMapper fileInfoMapper;
    @Resource
    private InterfaceModelMapper interfaceModelMapper;
    @Autowired
    private ISysDictTypeService sysDictTypeService;
    /**
     * 查询实体用况
     *
     * @param id 实体用况主键
     * @return 实体用况
     */
    @Override
    public InterfaceInfo selectInterfaceInfoById(Long id) {
        return interfaceInfoMapper.selectInterfaceInfoById(id);
    }

    protected void startPage()
    {
        if(RequestContextHolder.getRequestAttributes() != null) {
            PageDomain pageDomain = TableSupport.buildPageRequest();
            Integer pageNum = pageDomain.getPageNum();
            Integer pageSize = pageDomain.getPageSize();
            if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize)) {
                String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
                PageHelper.startPage(pageNum, pageSize, orderBy);
            }
        }
    }

    /**
     * 查询实体用况列表
     *
     * @param interfaceInfo 实体用况
     * @return 实体用况
     */
    @Override
    public List<InterfaceInfo> selectInterfaceInfoList(InterfaceInfo interfaceInfo) {
        List<Long> interfaceIds = null;
        if (StringUtils.isNotEmpty(interfaceInfo.getFieldModel())) {
            interfaceIds = interfaceModelMapper.queryInterfaceId(interfaceInfo.getFieldModel(), "1", interfaceIds);
        }
        if (StringUtils.isNotEmpty(interfaceInfo.getSelfFieldModel()) && (interfaceIds == null || interfaceIds.size() > 0)) {
            interfaceIds = interfaceModelMapper.queryInterfaceId(interfaceInfo.getSelfFieldModel(), "2", interfaceIds);
        }
        if (StringUtils.isNotEmpty(interfaceInfo.getAliasZhName()) && (interfaceIds == null || interfaceIds.size() > 0)) {
            interfaceIds = interfaceModelMapper.queryInterfaceId(interfaceInfo.getAliasZhName(), "3", interfaceIds);
        }
        if (StringUtils.isNotEmpty(interfaceInfo.getAliasEnName()) && (interfaceIds == null || interfaceIds.size() > 0)) {
            interfaceIds = interfaceModelMapper.queryInterfaceId(interfaceInfo.getAliasEnName(), "4", interfaceIds);
        }
        if (StringUtils.isNotEmpty(interfaceInfo.getOtherModel()) && (interfaceIds == null || interfaceIds.size() > 0)) {
            interfaceIds = interfaceModelMapper.queryInterfaceId(interfaceInfo.getOtherModel(), "5", interfaceIds);
        }
        if (StringUtils.isNotEmpty(interfaceInfo.getIsAllowEmptyModel()) && (interfaceIds == null || interfaceIds.size() > 0)) {
            interfaceIds = interfaceModelMapper.queryInterfaceId(interfaceInfo.getIsAllowEmptyModel(), "6", interfaceIds);
        }
        if (StringUtils.isNotEmpty(interfaceInfo.getInterfaceModel()) && (interfaceIds == null || interfaceIds.size() > 0)) {
            interfaceIds = interfaceModelMapper.queryInterfaceId(interfaceInfo.getInterfaceModel(),"7", interfaceIds);
        }
        if (StringUtils.isNotEmpty(interfaceInfo.getInterfaceAllowEmptyModel()) && (interfaceIds == null || interfaceIds.size() > 0)) {
            interfaceIds = interfaceModelMapper.queryInterfaceId(interfaceInfo.getInterfaceAllowEmptyModel(),"8",interfaceIds);
        }
        if (StringUtils.isNotEmpty(interfaceInfo.getDictValue()) && (interfaceIds == null || interfaceIds.size() > 0)) {
            interfaceIds = interfaceModelMapper.queryInterfaceId(interfaceInfo.getDictValue(),"9",interfaceIds);
        } else if (StringUtils.isNotEmpty(interfaceInfo.getDictType()) && (interfaceIds == null || interfaceIds.size() > 0)) {
            List<SysDictData> sysDictData = this.sysDictTypeService.selectDictDataByType(interfaceInfo.getDictType());
            if (CollectionUtil.isNotEmpty(sysDictData)) {
                List<String> dictValues = sysDictData.stream().map(SysDictData::getDictValue).collect(Collectors.toList());
                interfaceIds = interfaceModelMapper.queryInterfaceId2(dictValues,"9", interfaceIds);
            } else {
                return new ArrayList<>();
            }
        }
        if (interfaceIds == null || interfaceIds.size() > 0) {
            if (interfaceIds != null && interfaceIds.size() > 0) {
                interfaceInfo.setIds(interfaceIds);
            }
            startPage();
            return interfaceInfoMapper.selectInterfaceInfoList(interfaceInfo);
        } else {
            return new ArrayList<>();
        }
    }

    public List<InterfaceInfo> selectInterfaceInfoByIds(List<Long> ids) {
        List<InterfaceInfo> list = interfaceInfoMapper.selectInterfaceInfoByIds(ids);
        if (null == list || list.size() == 0) {
            return null;
        }
        // 循环查询出用户角色
        list.forEach(a -> {
            UserDataAuth userDataAuth = new UserDataAuth();
            userDataAuth.setObjectId(a.getId());
            userDataAuth.setObjectType("5");
            List<UserDataAuth> userList = userDataAuthMapper.selectUserDataAuthListV2(userDataAuth);
            if (null == userList || userList.size() == 0) {
                return;
            }
            a.setUserList(userList);
        });
        return list;
    }

    /**
     * 新增实体用况
     *
     * @param interfaceInfo 实体用况
     * @return 结果
     */
    @Override
    public int insertInterfaceInfo(InterfaceInfo interfaceInfo) {
        interfaceInfo.setCreateTime(DateUtils.getNowDate());
        interfaceInfo.setCreateBy(ShiroUtils.getLoginName());

        InterfaceInfo info = new InterfaceInfo();
        info.setInterfaceUrl(interfaceInfo.getInterfaceUrl());
        info.setFbType(interfaceInfo.getFbType());
        List<InterfaceInfo> interfaceInfoList = this.interfaceInfoMapper.selectInterfaceInfoList(info);
        if (CollectionUtil.isNotEmpty(interfaceInfoList)) {
            throw new BusinessException("接口地址重复");
        }

        int i = interfaceInfoMapper.insertInterfaceInfo(interfaceInfo);
        if (interfaceInfo.getPageId() != null) {
            InterfaceObject interfaceObject = new InterfaceObject();
            interfaceObject.setInterfaceId(interfaceInfo.getId());
            interfaceObject.setObjectId(interfaceInfo.getPageId());
            interfaceObject.setObjectType("2");
            this.interfaceObjectMapper.insertInterfaceObject(interfaceObject);
        }
        return i;

    }
    @Resource
    private InterfaceObjectMapper interfaceObjectMapper;
    @Resource
    private ProductVersionObejctMapper productVersionObejctMapper;
    @Resource
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;
    @Resource
    private IInterfaceGeneratorService interfaceGeneratorService;

    /**
     * 修改实体用况
     *
     * @param interfaceInfo 实体用况
     * @return 结果
     */
    @Override
    @Transactional
    public int updateInterfaceInfo(InterfaceInfo interfaceInfo) {
        interfaceInfo.setUpdateTime(DateUtils.getNowDate());
        interfaceInfo.setUpdateBy(ShiroUtils.getLoginName());
        this.productVersionObejctMapper.removeByObjectId(interfaceInfo.getId(), "5");
        if (CollectionUtil.isNotEmpty(interfaceInfo.getProductIds())) {
            List<ProductVersionObejct> list = new ArrayList<>();
            for (Long productId : interfaceInfo.getProductIds()) {
                if(productId ==null || productId == 0) continue;
                ProductVersionObejct object = new ProductVersionObejct();
                object.setObjectId(interfaceInfo.getId());
                object.setTableProductVersionId(productId);
                object.setObjectType("5");
                list.add(object);
            }
            if (list.size() > 0) {
                this.productVersionObejctMapper.batchSave(list);
            }
        }
        if (interfaceInfo.getFbType().equals("2")) {
            InterfaceInfo info = new InterfaceInfo();
            info.setInterfaceUrl(interfaceInfo.getInterfaceUrl());
            info.setFbType("2");
            List<InterfaceInfo> interfaceInfoList = this.interfaceInfoMapper.selectInterfaceInfoList(info);
            if (CollectionUtil.isNotEmpty(interfaceInfoList)) {
                for (InterfaceInfo interfaceInfo1 : interfaceInfoList) {
                    if (!interfaceInfo1.getId().equals(interfaceInfo.getId())) {
                        throw new BusinessException("接口地址重复");
                    }
                }
            }
            InterfaceObject intObject = new InterfaceObject();
            intObject.setInterfaceId(interfaceInfo.getId());
            intObject.setObjectType("2");
            List<InterfaceObject> objectList = this.interfaceObjectMapper.selectInterfaceObjectList(intObject);
            Map<Long, List<InterfaceObject>> collect = null;
            if (CollectionUtil.isNotEmpty(objectList)) {
                collect = objectList.stream().collect(Collectors.groupingBy(InterfaceObject::getObjectId));
            }
            // 删除页面和用况
            this.interfaceObjectMapper.remove(interfaceInfo.getId(), "2", null);
            this.interfaceObjectMapper.remove(interfaceInfo.getId(), "3", null);

            if (CollectionUtil.isNotEmpty(interfaceInfo.getPageIds())) {
                List<InterfaceObject> interfaceObjectList = new ArrayList<>();
                for (Long pageId : interfaceInfo.getPageIds()) {
                    if(pageId == 0) continue;
                    InterfaceObject interfaceObject = new InterfaceObject();
                    interfaceObject.setObjectId(pageId);
                    interfaceObject.setInterfaceId(interfaceInfo.getId());
                    interfaceObject.setObjectType("2");
                    interfaceObject.setSortNum(0);
                    if (collect != null) {
                        List<InterfaceObject> interfaceObjectList1 = collect.get(pageId);
                        if (CollectionUtil.isNotEmpty(interfaceObjectList1)) {
                            interfaceObject.setSortNum(interfaceObjectList1.get(0).getSortNum());
                        }
                    }
                    interfaceObjectList.add(interfaceObject);
                }
                if (interfaceObjectList.size() > 0) {
                    this.interfaceObjectMapper.batchSave(interfaceObjectList);
                }
            }

            if (interfaceInfo.getUsageId() != null && interfaceInfo.getUsageId() > 0) {
                //this.interfaceObjectMapper.removeByObject(Arrays.asList(interfaceInfo.getUsageId()), "3");
                InterfaceObject interfaceObject = new InterfaceObject();
                interfaceObject.setObjectId(interfaceInfo.getUsageId());
                interfaceObject.setInterfaceId(interfaceInfo.getId());
                interfaceObject.setObjectType("3");
                this.interfaceObjectMapper.insertInterfaceObject(interfaceObject);
            }
        } else {
            InterfaceInfo info = new InterfaceInfo();
            info.setInterfaceUrl(interfaceInfo.getInterfaceUrl());
            info.setFbType(interfaceInfo.getFbType());
            List<InterfaceInfo> interfaceInfoList = this.interfaceInfoMapper.selectInterfaceInfoList(info);
            if (CollectionUtil.isNotEmpty(interfaceInfoList)) {
                for (InterfaceInfo interfaceInfo1 : interfaceInfoList) {
                    if (!interfaceInfo1.getId().equals(interfaceInfo.getId())) {
                        throw new BusinessException("接口方法重复");
                    }
                }
            }
        }
        interfaceInfo.setFbType(null);
        int i = interfaceInfoMapper.updateInterfaceInfo(interfaceInfo);
        //if ("Y".equals(interfaceInfo.getIsEditTest())) {
        interfaceGeneratorService.generator(Arrays.asList(interfaceInfo.getId()));
        //}
        return i;
    }

    @Resource
    private UserDataAuthMapper userDataAuthMapper;
    @Resource
    private InterfaceParamsFieldRelationMapper interfaceParamsFieldRelationMapper;
    @Resource
    private ParamsFieldRelationMapper paramsFieldRelationMapper;
    @Resource
    private ParamsMapper paramsMapper;
    @Resource
    private VirtualUsageMapper virtualUsageMapper;

    /**
     * 批量删除实体用况
     *
     * @param ids 需要删除的实体用况主键
     * @return 结果
     */
    @Override
    public int deleteInterfaceInfoByIds(String ids) {
        List<Long> interfaceIds = Convert.toLongList(ids);
        userDataAuthMapper.removeByObjectId(interfaceIds, "5");
        InterfaceInfo interfaceInfo = new InterfaceInfo();
        interfaceInfo.setIds(interfaceIds);
        List<InterfaceInfo> interfaceInfoList = this.interfaceInfoMapper.selectInterfaceInfoList(interfaceInfo);
        List<Long> dIds = new ArrayList<>();
        List<Long> bIds = new ArrayList<>();
        for (InterfaceInfo info : interfaceInfoList) {
            if (info.getFbType().equals("1")) {
                dIds.add(info.getId());
            } else {
                bIds.add(info.getId());
            }
        }
        deleteParam(dIds, "1");
        deleteParam(bIds, "2");
        VirtualUsage virtualUsage = new VirtualUsage();
        virtualUsage.setRelationInterfaceIds(new HashSet<>(interfaceIds));
        List<VirtualUsage> virtualUsages = this.virtualUsageMapper.selectVirtualUsageList(virtualUsage);
        List<Long> vIds = virtualUsages.stream().map(a -> a.getId()).collect(Collectors.toList());
        deleteParam(vIds, "3");
        //删除虚拟用况
        this.virtualUsageMapper.deleteVirtualUsageByRelationInterfaceId(interfaceIds);

        return interfaceInfoMapper.deleteInterfaceInfoByIds(Convert.toStrArray(ids));
    }

    private void deleteParam(List<Long> interfaceIds, String type) {
        if (!CollectionUtil.isNotEmpty(interfaceIds)) return;
        InterfaceParamsFieldRelation interfaceParamsFieldRelation = new InterfaceParamsFieldRelation();
        interfaceParamsFieldRelation.setInterfaceIds(interfaceIds);
        interfaceParamsFieldRelation.setType(type);
        List<InterfaceParamsFieldRelation> interfaceParamsFieldRelations = this.interfaceParamsFieldRelationMapper.selectInterfaceParamsFieldRelationList(interfaceParamsFieldRelation);
        List<Long> idList = new ArrayList<>();
        List<Long> paramsIdList = new ArrayList<>();
        List<Long> paramsFieldIdList = new ArrayList<>();
        for (InterfaceParamsFieldRelation paramsFieldRelation : interfaceParamsFieldRelations) {
            idList.add(paramsFieldRelation.getId());
            if (paramsFieldRelation.getObjectType().equals("2")) {
                paramsIdList.add(paramsFieldRelation.getObjectId());
                paramsFieldIdList.add(paramsFieldRelation.getId());
            }
        }
        // 删除参数
        if (idList.size() > 0) {
            this.interfaceParamsFieldRelationMapper.removeInterfaceId(idList);
        }
        // 删除参数集
        if (paramsIdList.size() > 0) {
            // this.paramsMapper.removeByIds(paramsIdList);
            // 删除参数集下字段
            this.paramsFieldRelationMapper.removeByInterfaceDataListId(paramsFieldIdList);
        }
        //删除表关系
        this.interfaceObjectMapper.removeByObject(interfaceIds, "1");
    }

    /**
     * 删除实体用况信息
     *
     * @param id 实体用况主键
     * @return 结果
     */
    @Override
    public int deleteInterfaceInfoById(Long id) {
        return interfaceInfoMapper.deleteInterfaceInfoById(id);
    }

    @Override
    public List<InterfaceInfo> selectSubInterfaces(Long interfaceId) {
        return interfaceInfoMapper.selectSubInterfaces(interfaceId);
    }

    @Override
    public List<Usage> selectUsageList(List<Long> ids) {
        return this.interfaceInfoMapper.selectUsageList(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean editDubboInterface(DubboInterfaceAddReq dubboInterfaceAddReq) {
        InterfaceInfo interfaceInfo = new InterfaceInfo();
        BeanUtils.copyProperties(dubboInterfaceAddReq, interfaceInfo);
        interfaceInfo.setFbType("1");
        interfaceInfo.setUpdateBy(ShiroUtils.getLoginName());
        interfaceInfo.setUpdateTime(new Date());
        this.interfaceInfoMapper.updateInterfaceInfo(interfaceInfo);

        // 转换参数
        List<InterfaceParamsFieldRelation> interfaceParamsFieldRelationList =
                convertParameter(dubboInterfaceAddReq.getInterfaceParamReqList(), "1", interfaceInfo.getId());
        if (!CollectionUtil.isNotEmpty(interfaceParamsFieldRelationList)) {
            return false;
        }
        List<Long> interfaceDataListIds = new ArrayList<>();
        List<Long> ids = new ArrayList<>();
        for (InterfaceParamsFieldRelation interfaceParamsFieldRelation : interfaceParamsFieldRelationList) {
            if (interfaceParamsFieldRelation.getId() != null) {
                if ("2".equals(interfaceParamsFieldRelation.getObjectType())) {
                    interfaceDataListIds.add(interfaceParamsFieldRelation.getId());
                }
                ids.add(interfaceParamsFieldRelation.getId());
            }
        }
        // 删除数据集与字段关系
        if (CollectionUtil.isNotEmpty(interfaceDataListIds)) {
            this.paramsFieldRelationMapper.removeByInterfaceDataListId(interfaceDataListIds);
        }
        // 删除接口与字段关系
        if (CollectionUtil.isNotEmpty(ids)) {
            this.interfaceParamsFieldRelationMapper.removeInterfaceId(ids);
        }
        // 保存接口与字段关系
        this.interfaceParamsFieldRelationMapper.batchSave(interfaceParamsFieldRelationList);

        // 保存数据集与字段关系
        saveParamsField(interfaceParamsFieldRelationList);
        return true;
    }

    @Override
    public List<InterfaceParam> paramList(String interfaceType, Long interfaceId, String type) {
        // 接口与字段
        List<InterfaceParam> listFieldParam = this.interfaceInfoMapper.listFieldParam(interfaceType, interfaceId, type);
        // 接口与数据集
        List<InterfaceParam> listParams = this.interfaceInfoMapper.listParams(interfaceType, interfaceId, type);

        List<InterfaceParam> paramList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(listFieldParam)) {
            paramList.addAll(listFieldParam);
        }
        if (CollectionUtil.isNotEmpty(listParams)) {
            paramList.addAll(listParams);
        }
        paramList.sort(Comparator.comparingInt(InterfaceParam::getSortNum));
        return paramList;
    }

    @Override
    public List<FieldInfo> fieldList(String interfaceType, Long interfaceId, String type) {
        return this.interfaceInfoMapper.fieldList(interfaceType, interfaceId, type);
    }

    @Override
    public List<TableInfo> tableList(String interfaceType, Long interfaceId) {
        return this.interfaceInfoMapper.tableList(interfaceType, interfaceId);
    }

    @Override
    public void saveDubboParam(DubboParamReq dubboParamReq) {
        int maxSort = this.interfaceParamsFieldRelationMapper.getMaxSort(dubboParamReq.getType(), dubboParamReq.getInterfaceId(),dubboParamReq.getIsInParam()) + 1;
        List<InterfaceParamsFieldRelation> interfaceParamsFieldRelationList = new ArrayList<>();
        for (Long objectId : dubboParamReq.getFieldId()) {
            InterfaceParamsFieldRelation interfaceParamsFieldRelation = new InterfaceParamsFieldRelation();
            interfaceParamsFieldRelation.setType(dubboParamReq.getType());
            interfaceParamsFieldRelation.setInterfaceId(dubboParamReq.getInterfaceId());
            interfaceParamsFieldRelation.setObjectType(dubboParamReq.getObjectType());
            interfaceParamsFieldRelation.setObjectId(objectId);
            interfaceParamsFieldRelation.setIsInParam(dubboParamReq.getIsInParam());
            interfaceParamsFieldRelation.setSortNum(maxSort++);
            interfaceParamsFieldRelationList.add(interfaceParamsFieldRelation);
        }
        this.interfaceParamsFieldRelationMapper.batchSave(interfaceParamsFieldRelationList);
    }

    @Autowired
    private ITableInfoService tableInfoService;

    @Override
    public void saveMethodParam(DubboParamReq dubboParamReq) {
        int maxSort = this.interfaceParamsFieldRelationMapper.getMaxSort(dubboParamReq.getType(), dubboParamReq.getInterfaceId(),dubboParamReq.getIsInParam()) + 1;
        List<InterfaceParamsFieldRelation> interfaceParamsFieldRelationList = new ArrayList<>();
        List<FieldInfo> fieldInfoList = this.tableInfoService.selectFieldInfoByFieldId(dubboParamReq.getFieldId());
        Map<Long, List<FieldInfo>> fieldInfoMap = fieldInfoList.stream().collect(Collectors.groupingBy(FieldInfo::getId));
        InterfaceInfo interfaceInfo = this.interfaceInfoMapper.selectInterfaceInfoById(dubboParamReq.getInterfaceId());
        for (Long objectId : dubboParamReq.getFieldId()) {
            InterfaceParamsFieldRelation interfaceParamsFieldRelation = new InterfaceParamsFieldRelation();
            interfaceParamsFieldRelation.setType(dubboParamReq.getType());
            interfaceParamsFieldRelation.setInterfaceId(dubboParamReq.getInterfaceId());
            interfaceParamsFieldRelation.setObjectType(dubboParamReq.getObjectType());
            interfaceParamsFieldRelation.setObjectId(objectId);
            interfaceParamsFieldRelation.setIsInParam(dubboParamReq.getIsInParam());
            interfaceParamsFieldRelation.setSortNum(maxSort++);
            List<FieldInfo> fieldInfos = fieldInfoMap.get(objectId);
            interfaceParamsFieldRelation.setFieldDescription(fieldInfos.get(0).getFieldDescription());
            interfaceParamsFieldRelation.setDesignContent(fieldInfos.get(0).getDesignContent());
            if ("1".equals(interfaceInfo.getOptType()) || "2".equals(interfaceInfo.getOptType())) {
                interfaceParamsFieldRelation.setIsRequired(fieldInfos.get(0).getIsRequired());
                interfaceParamsFieldRelation.setIsRequiredParent(fieldInfos.get(0).getIsRequired());
            }
            if (StringUtils.isNotEmpty(fieldInfos.get(0).getDesignContent())) {
                interfaceParamsFieldRelation.setIsUpdate("N");
            } else {
                interfaceParamsFieldRelation.setIsUpdate("Y");
            }
            interfaceParamsFieldRelationList.add(interfaceParamsFieldRelation);
        }
        this.interfaceParamsFieldRelationMapper.batchSave(interfaceParamsFieldRelationList);
    }

    @Override
    public void updateSort(DubboInterfaceAddReq dubboInterfaceAddReq) {
        // 转换参数
        List<InterfaceParamsFieldRelation> interfaceParamsFieldRelationList =
                convertParameter(dubboInterfaceAddReq.getInterfaceParamReqList(), dubboInterfaceAddReq.getFbType(), dubboInterfaceAddReq.getId());
        if (!CollectionUtil.isNotEmpty(interfaceParamsFieldRelationList)) {
            return ;
        }
        List<ParamsFieldRelation> paramsFieldRelationList = new ArrayList<>();
        for (InterfaceParamsFieldRelation interfaceParamsFieldRelation : interfaceParamsFieldRelationList) {
            if (interfaceParamsFieldRelation.getParamsFieldRelationList() != null) {
                paramsFieldRelationList.addAll(interfaceParamsFieldRelation.getParamsFieldRelationList());
            }
        }
        this.interfaceParamsFieldRelationMapper.updateSort(interfaceParamsFieldRelationList);
        if (paramsFieldRelationList.size() > 0) {
            this.paramsFieldRelationMapper.updateSort(paramsFieldRelationList);
        }
    }

    @Override
    public List<InterfaceInfo> dubboList(InterfaceInfo interfaceInfo) {
        return this.interfaceInfoMapper.dubboList(interfaceInfo);
    }

    @Override
    public List<InterfaceParam> listInterfaceFieldParam(ListInterfaceFieldParamReq listInterfaceFieldParamReq) {
        List<InterfaceParam> listFieldParam = this.interfaceInfoMapper.listInterfaceFieldParam(listInterfaceFieldParamReq);
        return listFieldParam;
    }

    @Override
    public List<InterfaceParam> listInterfaceParams(ListInterfaceFieldParamReq listInterfaceFieldParamReq) {
        List<InterfaceParam> listParams = this.interfaceInfoMapper.listInterfaceParams(listInterfaceFieldParamReq);
        return listParams;
    }

    @Override
    public List<InterfaceParam> listInterfaceParamsField(ListInterfaceFieldParamReq listInterfaceFieldParamReq) {
        List<InterfaceParam> listInterfaceParamsField = this.interfaceInfoMapper.listInterfaceParamsField(listInterfaceFieldParamReq);
        return listInterfaceParamsField;
    }

    @Override
    public void addBffImg(BffImgReq bffImgReq) {
        if (CollectionUtil.isNotEmpty(bffImgReq.getFileInfList())) {
            for (FileInf fileInf : bffImgReq.getFileInfList()) {
                fileInf.setObjectType("1");
                fileInf.setObjectId(bffImgReq.getInterfaceId());
            }
            this.fileInfoMapper.batchSave(bffImgReq.getFileInfList());
        }
    }

    @Override
    public List<InterfaceInfo> usedInterfaceList(Long id) {
        return this.interfaceInfoMapper.usedInterfaceList(id);
    }

    @Override
    public List<InterfaceParam> virtualUsageInParamList(String interfaceType, Long interfaceId, Long processBranchId, Long virtualUsageGroupId) {
        // 接口与字段
        List<InterfaceParam> listFieldParam = this.interfaceInfoMapper.listVirtualUsageFieldParam(interfaceType, interfaceId, processBranchId,virtualUsageGroupId);
        // 接口与数据集
        List<InterfaceParam> listParams = this.interfaceInfoMapper.listVirtualUsageParams(interfaceType, interfaceId, processBranchId,virtualUsageGroupId);

        List<InterfaceParam> paramList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(listFieldParam)) {
            paramList.addAll(listFieldParam);
        }
        if (CollectionUtil.isNotEmpty(listParams)) {
            paramList.addAll(listParams);
        }
        paramList.sort(Comparator.comparingInt(InterfaceParam::getSortNum));
        return paramList;
    }

    @Override
    public List<InterfaceParam> paramRequiredList(String type, Long interfaceInfoId, String isInParam) {
        return this.interfaceInfoMapper.paramRequiredList(type, interfaceInfoId, isInParam);
    }

    private void saveParamsField(List<InterfaceParamsFieldRelation> interfaceParamsFieldRelationList) {
        if (CollectionUtil.isNotEmpty(interfaceParamsFieldRelationList)) {
            List<ParamsFieldRelation> paramsFieldRelationList = new ArrayList<>();
            for (InterfaceParamsFieldRelation interfaceParamsFieldRelation : interfaceParamsFieldRelationList) {
                if (CollectionUtil.isNotEmpty(interfaceParamsFieldRelation.getParamsFieldRelationList())) {
                    for (ParamsFieldRelation paramsFieldRelation : interfaceParamsFieldRelation.getParamsFieldRelationList()) {
                        paramsFieldRelation.setInterfaceDataListId(interfaceParamsFieldRelation.getId());
                        paramsFieldRelation.setCreateBy(ShiroUtils.getLoginName());
                        paramsFieldRelation.setCreateTime(new Date());
                        paramsFieldRelationList.add(paramsFieldRelation);
                    }
                }
            }
            if (paramsFieldRelationList.size() > 0) {
                this.paramsFieldRelationMapper.batchSave(paramsFieldRelationList);
            }
        }
    }

    private List<InterfaceParamsFieldRelation> convertParameter(List<InterfaceParamReq> interfaceParamReqList,
                                                                String type, Long interfaceId) {
        List<InterfaceParamsFieldRelation> results = null;
        if (CollectionUtil.isNotEmpty(interfaceParamReqList)) {
            results = new ArrayList<>();
            for (InterfaceParamReq interfaceParamReq : interfaceParamReqList) {
                InterfaceParamsFieldRelation interfaceParamsFieldRelation = new InterfaceParamsFieldRelation();
                BeanUtils.copyProperties(interfaceParamReq, interfaceParamsFieldRelation);
                interfaceParamsFieldRelation.setType(type);
                interfaceParamsFieldRelation.setInterfaceId(interfaceId);
                interfaceParamsFieldRelation.setCreateBy(ShiroUtils.getLoginName());
                interfaceParamsFieldRelation.setCreateTime(new Date());
                results.add(interfaceParamsFieldRelation);
                if (CollectionUtil.isNotEmpty(interfaceParamReq.getParamsFieldReqList())) {
                    List<ParamsFieldRelation> paramsFieldRelationList = new ArrayList<>();
                    for (ParamsFieldReq paramsFieldReq : interfaceParamReq.getParamsFieldReqList()) {
                        ParamsFieldRelation paramsFieldRelation = new ParamsFieldRelation();
                        BeanUtils.copyProperties(paramsFieldReq, paramsFieldRelation);
                        paramsFieldRelation.setInterfaceDataListId(interfaceParamReq.getId());
                        paramsFieldRelationList.add(paramsFieldRelation);
                    }
                    interfaceParamsFieldRelation.setParamsFieldRelationList(paramsFieldRelationList);
                }
            }
        }
        return results;
    }
}
